(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		callback = callback || $.noop;
		loginInfo = loginInfo || {};
		loginInfo.usercode = loginInfo.usercode || '';
		loginInfo.password = loginInfo.password || '';
		if (loginInfo.usercode.length <= 0) {
			callback('账号不能为空');
			return
		}
		if (loginInfo.password.length <= 0) {
			callback('密码不能为空');
			return
		}
		
		// var user = JSON.parse(localStorage.getItem('$user') || '{}');
		// console.log(JSON.stringify(user))
		var paramDict = {
			'method': 'web.INMLoginComm:GetNursePassword',
			'params': JSON.stringify(loginInfo),
			'type': 'Method'
		}
		plus.nativeUI.showWaiting('正在验证...', {
			modal: false
		})
		$.DHCWebService(paramDict, function(result) {
			setTimeout(function() {
				plus.nativeUI.closeWaiting();
				var msg = result.msg.split('^')[0];
				if (msg == '1') {
					var userid = result.msg.split('^')[1];
					var userArg = {
						id: userid,
						APPLoginID: '',
						APPPerID: ''
					}
					return owner.getUserInfo(userArg, loginInfo.password, callback);
				} else {
					return callback(msg);
				}
			}, 300);
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			return callback(errorStr);
		});
	};
	owner.getUserInfo = function(userArg, psw, callback) {
		callback = callback || $.noop;
		userArg = userArg || {};
		// var user = JSON.parse(localStorage.getItem('$user') || '{}');
		// console.log(JSON.stringify(userArg))
		var paramDict = {
			'method': 'web.INMLoginComm:GetPersonByLoginId',
			'params': JSON.stringify(userArg),
			'type': 'RecMethod'
		}
		plus.nativeUI.showWaiting('正在获取用户信息', {
			modal: false
		})
		$.DHCWebService(paramDict, function(result) {
			setTimeout(function() {
				plus.nativeUI.closeWaiting();
				if (result.msgcode != "1") {
					return callback(result.msg);
				}
				//console.log(JSON.stringify(result))
				var info = result.data;
				if (!info.TomcatUrl || info.TomcatUrl == '') {
					// mui.toast('未获取到Tomcat地址')
					console.log('未获取到Tomcat地址'); 
				}
				return owner.createState(info, psw, callback);
			}, 500);
		}, function(errorStr) {
			plus.nativeUI.closeWaiting();
			return callback('用户信息获取错误！');
		});
	};

	owner.createState = function(userInfo, psw, callback) {
		var state = owner.getState();
		state = userInfo;
		state.usercode = userInfo.loginCode;
		state.password = psw;
		var APPMenu = state['APPMenu']
		var newMenu={}
		if (APPMenu) {
			try{
				APPMenu = APPMenu.replace(/'/g,'"')
			}catch(e){
				while (APPMenu.indexOf("'") != -1){
					APPMenu = APPMenu.replace("'",'"')
				}
			}
			APPMenu = JSON.parse(APPMenu)
			mui.each(APPMenu,function (index,dict) {
				var code = dict['MenuCode']
				if (code != '') {
					newMenu[code]=dict
				}
			})
		}
		state['APPMenu'] = newMenu
		owner.setState(state);
		// console.log(JSON.stringify(state))
		localStorage.setItem('$user', JSON.stringify(state));
		return callback();
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		var settings = owner.getSettings();
		// settings.gestures = '';
		owner.setSettings(settings);
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
}(mui, window.app = {}));
